import { defineStore } from 'pinia'
import { store } from '../index'
import { UserLoginType, DeptInfoType, UserInfoType } from '@/api/login/types'
import { ElMessageBox } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { loginOutApi } from '@/api/login'
import { useTagsViewStore } from './tagsView'
import router from '@/router'
import { useCrypto } from '@/hooks/web/useCrypto'
const { localEncrypt, localDecrypt } = useCrypto()

interface UserState {
  userInfo?: UserInfoType
  tokenKey: string
  token: string
  // roleRouters?: string[] | AppCustomRouteRecordRaw[]
  rememberMe: boolean
  loginInfo?: UserLoginType
  deptInfo?: DeptInfoType
}

export const useUserStore = defineStore('user', {
  state: (): UserState => {
    return {
      userInfo: undefined,
      tokenKey: 'Authorization',
      token: '',
      // roleRouters: [],
      // 记住我
      rememberMe: true,
      loginInfo: undefined,
      deptInfo: undefined
    }
  },
  getters: {
    getTokenKey(): string {
      return this.tokenKey
    },
    getToken(): string {
      return localDecrypt(this.token)
    },
    getUserInfo(): UserInfoType | undefined {
      return localDecrypt(this.userInfo)
    },
    getDeptInfo(): DeptInfoType | undefined {
      return localDecrypt(this.deptInfo)
    },

    // getRoleRouters(): string[] | AppCustomRouteRecordRaw[] | undefined {
    //   return localDecrypt(this.roleRouters)
    // },
    getRememberMe(): boolean {
      return this.rememberMe
    },
    getLoginInfo(): UserLoginType | undefined {
      return localDecrypt(this.loginInfo)
    }
  },
  actions: {
    setTokenKey(tokenKey: string) {
      this.tokenKey = tokenKey
    },
    setToken(token: string) {
      this.token = token ? localEncrypt(token) : ''
    },
    setUserInfo(userInfo?: UserInfoType) {
      this.userInfo = userInfo ? localEncrypt(userInfo) : undefined
    },
    setDeptInfo(deptInfo?: DeptInfoType) {
      this.deptInfo = deptInfo ? localEncrypt(deptInfo) : undefined
    },
    // setRoleRouters(roleRouters: string[] | AppCustomRouteRecordRaw[]) {
    //   this.roleRouters = localEncrypt(roleRouters)
    // },
    /**
     * 退出登录
     */
    async logoutConfirm() {
      return new Promise(async (resolve) => {
        const { t } = useI18n()
        ElMessageBox.confirm(t('common.loginOutMessage'), t('common.reminder'), {
          confirmButtonText: t('common.ok'),
          cancelButtonText: t('common.cancel'),
          type: 'warning'
        })
          .then(async () => {
            const res = await loginOutApi().catch(() => {})
            if (res) {
              this.reset()
              resolve(res)
            }
          })
          .catch(() => {})
      })
    },
    reset() {
      const tagsViewStore = useTagsViewStore()
      tagsViewStore.delAllViews()
      this.setToken('')
      this.setUserInfo(undefined)
      this.setDeptInfo(undefined)
      // this.setRoleRouters([])
      router.replace('/login')
    },
    logout() {
      this.reset()
    },
    setRememberMe(rememberMe: boolean) {
      this.rememberMe = rememberMe
    },
    setLoginInfo(loginInfo: UserLoginType | undefined) {
      this.loginInfo = localEncrypt(loginInfo)
    }
  },
  persist: true
})

export const useUserStoreWithOut = () => {
  return useUserStore(store)
}
